<!--  -->
<template>
  <div>
    <el-form :model="searchForm">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item
            label="姓名:"
            label-width="65px"
          >
            <el-input
              v-model="searchForm.userName"
              autocomplete="off"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item
            label="手机号:"
            label-width="65px"
          >
            <el-input
              v-model="searchForm.mobileNo"
              autocomplete="off"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <div class="grid-content" />
        </el-col>
        <el-col :span="5">
          <el-button
            type="primary"
            @click="dialogFormVisible = false"
          >查 询</el-button>
          <el-button
            type="primary"
            @click="dialogFormVisible = false"
          >清 空</el-button>
          <el-button
            v-if="addAuthorizationPermit"
            type="primary"
            @click="showAddAuthorizationForm()"
          >新 增</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="subsyName"
        label="会所"
      />
      <el-table-column
        prop="name"
        label="姓名"
      />
      <el-table-column
        prop="rollNames"
        label="手机号"
      />
      <el-table-column
        prop="mobile"
        label="性别"
      />

      <el-table-column
        fixed="right"
        label="操作"
        width="100"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
          >查看</el-button>
          <el-button
            v-if="editAuthorizationPermit"
            type="text"
            size="small"
            @click="showEditAuthorizationForm(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-size="10"
      :total="total"
      :pager-count="11"
      :current-page="currentPage"
      layout="prev, pager, next"
      @current-change="currentChange"
    />
    <el-dialog
      :visible.sync="dialogFormVisible"
      title="编辑菜单"
      width="500px"
    >
      <el-form :model="editForm">
        <el-form-item
          :label-width="formLabelWidth"
          label="用户名称："
        >

          <el-select
            v-model="editForm.userCode"
            placeholder="请选择"
            @change="selectUser"
          >
            <el-option
              v-for="item in userComboList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          :label-width="formLabelWidth"
          label="所属公司："
        >
          <el-select
            v-model="editForm.subsyCode"
            placeholder="请选择"
          >
            <el-option
              v-for="item in subsyComboList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          :label-width="formLabelWidth"
          label="绑定角色："
        >
          <el-select
            v-model="editForm.rollCodes"
            multiple
            collapse-tags
            placeholder="请选择"
          >
            <el-option
              v-for="item in roleComboList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addAuthorization()"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from '../../../utils/request'
import store from '../../../store'
export default {
  components: {},
  data() {
    return {
      total: 50,
      pageSize: 10,
      currentPage: 1,
      tableData: null,
      formLabelWidth: '120px',
      roleComboList: null,
      userComboList: null,
      subsyComboList: null,
      searchForm: {
        userName: ''
      },
      editForm: {
        rollCodes: null,
        userCode: null,
        subsyCode: null

      },
      dialogFormVisible: false,
      addAuthorizationPermit: false,
      editAuthorizationPermit: false
    }
  },
  created() {
    this.fetchData()
    if (store.getters.buttons.length > 0) {
      store.getters.buttons.forEach((item, index) => {
        if (item === 'authorization:create') {
          this.addAuthorizationPermit = true
        }
        if (item === 'authorization:update') {
          this.editAuthorizationPermit = true
        }
      })
    }
  },
  methods: {
    showAddAuthorizationForm() {
      this.dialogFormVisible = true
      this.initEditCombo()
    },
    showEditAuthorizationForm(data) {
      this.initEditCombo()
    },
    addAuthorization() {
      this.listLoading = true
      request({
        url: '/authorization/createAuthorization',
        method: 'post',
        data: {
          interUserCode: this.editForm.userCode,
          rollCodes: this.editForm.rollCodes,
          subsyCode: this.editForm.subsyCode
        }
      }).then(response => {
        this.listLoading = false
        this.dialogFormVisible = false
        this.fetchData()
      })
    },
    currentChange(currentPage) {
      this.currentPage = currentPage
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      request({
        url: '/authorization/getAuthorizationList',
        method: 'get',
        params: {
          currentPage: this.currentPage,
          pageSize: this.pageSize
        }
      }).then(response => {
        this.tableData = response.data.authorizationList
        this.total = response.data.count
        this.listLoading = false
      })
    },
    initEditCombo() {
      this.listLoading = true
      request({
        url: '/authorization/getAuthorizationEditCombo',
        method: 'get'
      }).then(response => {
        this.userComboList = response.data.userComboList
        this.roleComboList = response.data.roleComboList
        this.listLoading = false
      })
    },
    selectUser(val) {
      this.subsyComboList = null
      this.editForm.subsyCode = null
      this.listLoading = true
      request({
        url: '/authorization/getSubsyComboList',
        method: 'get',
        params: { interUserCode: val }
      }).then(response => {
        this.subsyComboList = response.data
        this.listLoading = false
      })
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
